<template>
  <div class="mod-config">
    <basic-container>
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-button v-if="permissions.integration_itgdatasource_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增
          </el-button>
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading">
          <el-table-column
            header-align="center"
            align="center"
            label="编号"
            width="90">
              <template slot-scope="scope">
                {{scope.$index+1}}
              </template>
          </el-table-column>
          <el-table-column
            prop="id"
            header-align="center"
            align="center"
            label="编号"
            v-if="show==true">
          </el-table-column>
          <el-table-column
            prop="name"
            header-align="center"
            align="center"
            label="数据源名称"
            width="130">
          </el-table-column>
          <el-table-column
            prop="note"
            header-align="center"
            align="center"
            label="数据源描述"
            width="160">
          </el-table-column>
          <el-table-column
            prop="type"
            header-align="center"
            align="center"
            label="数据源类型"
            width="130">
          </el-table-column>
          <el-table-column
            prop="connectionParams"
            header-align="center"
            align="center"
            label="数据源链接参数"
            width="150"
          >
            <el-popover trigger="click" placement="top" width="430">
              <pre v-html="json"></pre>
              <div slot="reference" >
                <el-button type="text">点击查看</el-button>
              </div>
            </el-popover>
          </el-table-column>
          <el-table-column
            prop="status"
            header-align="center"
            align="center"
            label="启动状态">
            <template slot-scope="scope">
              {{scope.row.status==0?'启用':'停用'}}
            </template>
          </el-table-column>
          <el-table-column
            prop="createUser"
            header-align="center"
            align="center"
            label="创建人">
          </el-table-column>
          <el-table-column
            prop="createTime"
            header-align="center"
            align="center"
            label="创建时间"
            width="160">
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            label="操作"
            width="170">
            <template slot-scope="scope">
              <el-button v-if="permissions.integration_itgdatasource_edit" type="text" size="small" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改
              </el-button>
              <el-button v-if="permissions.integration_itgdatasource_del" type="text" size="small" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    </basic-container>
  </div>
</template>

<script>
import {fetchList, delObj} from '@/api/integration/itgdatasource'
import TableForm from './itgdatasource-form'
import {mapGetters} from 'vuex'
import {remote} from "@/api/admin/dict";

export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      dbTypeValue: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
      json: '',
      show: false,
    }
  },
  components: {
    TableForm
  },
  created() {
    this.getDataList();
    // remote('datasource_type').then(response => {
    //   this.dbTypeValue = response.data.data;
    // });
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      fetchList(Object.assign({
        current: this.pageIndex,
        size: this.pageSize
      })).then(response => {
        this.dataList = response.data.data.records
        this.totalPage = response.data.data.total
        for(var i in this.dataList){
          var data = JSON.parse(this.dataList[i].connectionParams);
          this.json = this.syntaxHighlight(data);
        }
      })
      this.dataListLoading = false
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      this.$confirm('是否确认删除ID为' + id + '的内容', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getDataList()
      }).catch(() => {
      })
    },
    // 格式化json
    syntaxHighlight(json){
      if (typeof json != 'string') {
          json = JSON.stringify(json, undefined, 2);
        }
        json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
        return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
          var cls = 'number';
          if (/^"/.test(match)) {
            if (/:$/.test(match)) {
              cls = 'key';
            } else {
              cls = 'string';
            }
          } else if (/true|false/.test(match)) {
            cls = 'boolean';
          } else if (/null/.test(match)) {
            cls = 'null';
          }
          return '<span class="' + cls + '">' + match + '</span>';
        });
    }
  }
}
</script>
<style scoped>
  pre>>>.string { color: green; }
  pre>>>.key { color: red; }
</style>
